package com.ivan.lightcontrollerdemo;
 
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class BleService extends Service {
	BluetoothGatt mBleGatt;
	BluetoothAdapter mBluetoothAdapter;
	BluetoothManager mBluetoothManager;
	public static final String SERVICE_UUID = "0000FFF0-0000-1000-8000-00805f9b34fb";
	public static final String CHARETERISTIC_WRITEABLE_UUID = "0000FFF1-0000-1000-8000-00805f9b34fb";
	public static final String CHARETERISTIC_NOTIFI_UUID = "0000FFF2-0000-1000-8000-00805f9b34fb";
	BluetoothGattCallback mBleGattCallback = new BluetoothGattCallback() {
		public void onConnectionStateChange(BluetoothGatt gatt, int status,
				int newState) {
			if (newState == BluetoothProfile.STATE_CONNECTED) {
				mBleGatt.discoverServices();
			} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

			}
		}

		public void onServicesDiscovered(BluetoothGatt gatt, int status) {
			Log.i("services discover: ", "successful");
		};
	};

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		initialize();
	}

	public boolean initialize() {
		if (mBluetoothManager == null) {
			mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
			if (mBluetoothManager == null) {
				return false;
			}
			mBluetoothAdapter = mBluetoothManager.getAdapter();
			if (mBluetoothAdapter == null) {
				return false;

			}
		}
		return true;
	}

	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return new LocalBinder();
	}

	class LocalBinder extends Binder {
		BleService getBleService() {
			return BleService.this;
		}
	}

	public boolean connect(String address) {
		if (mBluetoothAdapter != null) {
			BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
			mBleGatt = device.connectGatt(getApplicationContext(), false,
					mBleGattCallback);
			if (mBleGatt != null)
				return true;
		}
		return false;
	}

	public boolean writeCharacteristic(String serviceUuid,
			String charatisticUuid, String content) {
		if (mBluetoothAdapter == null || mBleGatt == null) {
			return false;
		}
		BluetoothGattService gattService = mBleGatt.getService(java.util.UUID
				.fromString(serviceUuid));
		if (gattService == null) {
			return false;
		}
		BluetoothGattCharacteristic gattCharacteristic = gattService
				.getCharacteristic(java.util.UUID.fromString(charatisticUuid));
		if (gattCharacteristic == null) {
			return false;
		}
		byte[] value = null;
		try {
			value = Hex.decodeHex(content.toCharArray());
		} catch (DecoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		gattCharacteristic.setValue(value);

		return mBleGatt.writeCharacteristic(gattCharacteristic);
	}

	public void closeGatt() {
		if (mBleGatt != null)
			mBleGatt.close();
	}
}
